<?php
if (!defined('FosfoaSystem')) {exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class products extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
        $this->navigation_current = 'categories';
        $this->categories = spClass('categoriesModel')->spLinker()->findAll(null,
            'sort_order asc,categories_id asc');
    }
    function index()
    {
        $this->tt = "产品管理";
        $categories_id = $this->spArgs("categories_id", 0);
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 12);
        $obj = spClass("productsModel");
        $obj2 = spClass("commentsModel");
        if ($categories_id == 0) {
            $conditions = array('1' => 1);
        } else {
            $conditions = array('master_categories_id' => $categories_id);
        }
        $arr = $obj->spLinker()->spPager($current_page, $page_size)->findAll($conditions,
            'products_id desc');
        foreach ($arr as $k => $v) {
            if ($obj2->ispl($v['products_id'])) {
                $arr[$k]['pl'] = 1;
            }
        }
        $arrBar = $obj->spLinker()->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->categories_id = $categories_id;
        $this->display("admin/products_index.html");
    }
    function select_Relate()
    {
        $this->tt = "关联商品";
        $news_Relate = $this->spArgs('news_Relate');
        $type = $this->spArgs('type');
        $this->news_Relate = $news_Relate;
        $title = $this->spArgs("title");
        $categories_id = $this->spArgs("parent_id", 0);
        $this->parent_id = $categories_id;

        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 40);

        $obj = spClass("productsModel");
        if ($categories_id == 0) {
            if ($news_Relate <> '' && $type == 'news') {
                $conditions = 'products_id in (' . $news_Relate . ')';
            } else {
                $conditions = array('1' => 1);
            }
        } else {
            $conditions = array('master_categories_id' => $categories_id);
        }
        $products_arr = $obj->spLinker()->spPager($current_page, $page_size)->findAll($conditions,
            'products_id desc');

        $arrBar = $obj->spLinker()->spPager()->getPager();
        $this->tPageBar = $arrBar;
        if ($news_Relate <> '') {
            $relatearr = explode(',', $news_Relate);
            foreach ($products_arr as $k => $v) {
                if (in_array($v[products_id], $relatearr)) {
                    $products_arr[$k][relate] = true;
                }
            }
        }
        $this->products_arr = $products_arr;
        //分类
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;

        $this->display("admin/products_select_Relate.html");
    }
    function add()
    {
        $categories_id = $this->spArgs("categories_id", 0);
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->action = 'add';
        $this->parent_id = $categories_id;
        $this->addtitle = "添加产品";
        $Attribs = ATTRIBS;
        $Attribs = trim($Attribs);
        $arr = explode(chr(13), $Attribs);
        //dump($arr);

        foreach ($arr as $k => $v) {
            $arr[$k] = explode('|', trim($v));
            foreach ($arr[$k] as $k2 => $v2) {
                $arr2[$k][$k2] = explode(':', trim($v2));
            }
        }

        $this->size = $arr2;
        //$this->size = array(0 => '鞋类', 1 => '包类', 2 => '衣服');
        $this->display("admin/products_add.html");
    }
    function edit()
    {
        $Attribs = ATTRIBS;
        $Attribs = trim($Attribs);
        $arr = explode(chr(13), $Attribs);
        foreach ($arr as $k => $v) {
            $arr[$k] = explode('|', trim($v));
            foreach ($arr[$k] as $k2 => $v2) {
                $arr2[$k][$k2] = explode(':', trim($v2));
            }
        }

        $this->size = $arr2;
        //$this->size = array(0 => '鞋类', 1 => '包类', 2 => '衣服');
        $products_id = $this->spArgs("products_id", 0);
        $Obj = spClass('productsModel');
        $conditions = array("products_id" => $products_id);
        $products = $Obj->spLinker()->find($conditions);

        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->products = $products;
        $this->parent_id = $products['master_categories_id'];
        $allimg = $products['products_allimg'];

        if ($allimg <> "") {
            $allimg = unserialize($allimg);
            $i = count($allimg) - 1;
            foreach ($allimg as $key => $value) {
                //echo $key;
                if ($key == $i) {
                    $img .= $value;
                } else {
                    $img .= $value . '|';
                }
            }
            $this->img = $img;
        }

        //$allimg = explode('|', $products['products_allimg']);
        $this->allimg = $allimg;
        $this->addtitle = "修改产品";

        $this->display("admin/products_add.html");
    }
    function save()
    {
        $strAction = $this->spArgs("action");
        $allimg = $this->spArgs("allimg");
        $allimg = explode('|', $allimg);
        $allimg = array_unique($allimg);
        $allimg = serialize($allimg);
        $seokey = $this->spArgs("products_seokeywords");
        $name = $this->spArgs("name");
        $title = $this->spArgs("title");
        $alias = $this->spArgs('alias');
        $data = array(
            "master_categories_id" => $this->spArgs("parent_id"),
            "products_price" => $this->spArgs("products_price", 0),
            "products_price_discounted" => $this->spArgs("products_price_discounted", 0),
            "products_size" => $this->spArgs("products_size", 0),
            "products_num" => $this->spArgs("products_num", 0),
            'alias' => $alias,
            'type' => $this->spArgs('type',0),
            "detail" => array(
                "products_name" => $name,
                'products_title' => $title,
                "products_description" => $this->spArgs("content"),
                "products_seokeywords" => $seokey,
                "products_seodescription" => $this->spArgs("products_seodescription"),
                ),
            "products_allimg" => $allimg,
            "products_image" => $this->spArgs("uploadList"));

        $Obj = spClass('productsModel');
        $tagobj = spClass('tagModel');
        if ($strAction == 'add') {

            $data['products_date_added'] = time();
            $products_id = $Obj->spLinker()->create($data);
            $msg = "产品" . $data[detail][products_name] . "添加成功";
        } else {


            $data['products_last_modified'] = time();
            $products_id = $this->spArgs("id");
            $condition = array('products_id' => $products_id);
            $Obj->spLinker()->update($condition, $data);
            $msg = "产品" . $data[detail][products_name] . "修改成功";
        }
        $tag = spClass('tagModel')->Tagsave($seokey, 2);
        Savelog($msg);
        $result = array(
            'status' => 'y',
            'info' => $msg,
            );
        echo json_encode($result);
    }
    function del()
    {
        $Obj = spClass("productsModel");
        $Obj2 = spClass('commentsModel');
        $id2 = $this->spArgs("id", 0);
        $cid = $this->spArgs("cid", 0);
        if ($id2 == 0) {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);
            if (is_array($idsarray)) {

                foreach ($idsarray as $id) {
                    $conditions = array('products_id' => $id);

                    $arr = $Obj->find($conditions, '', 'products_id,products_allimg');
                    $allimg = unserialize($arr['products_allimg']);
                    foreach ($allimg as $img) {

                        if (file_exists("upload/" . $delimg)) {
                            @unlink("upload/" . $delimg);
                        }
                    }

                    if ($Obj->spLinker()->delete($conditions)) // 删除记录
                        {
                        $Obj2->delete($conditions); //删除评论
                    }
                }
            }

        } else {
            $conditions = array('products_id' => $id2);

            $arr = $Obj->find($conditions, '', 'products_id,products_allimg');
            $allimg = unserialize($arr['products_allimg']);
            foreach ($allimg as $img) {
                if (file_exists("upload/" . $delimg)) {
                    @unlink("upload/" . $delimg);
                }
            }


            if ($Obj->spLinker()->delete($conditions)) // 删除记录
                {
                $Obj2->delete($conditions); //删除评论
            }
        }
        $msg = "删除商品" . $id2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl('products', 'index', array('categories_id' =>
                $cid)));
    }

    function checkalias()
    {
        $id = $this->spArgs('pid');
        $alias = $this->spArgs('param');
        if ($alias <> '') {
            $aliasobj = spClass('productsModel')->aliascheck($alias, $id);
            if ($aliasobj) {
                echo "y";
            } else {
                echo "存在重复别名,请更换！";
            }
        } else {
            echo "请输入网址别名！";
        }
    }
}